package com.service.creditcard.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.model.pojo.creditcard.CreditRecord;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import com.client.model.vo.userVo.ArUserVo;
import com.service.creditcard.feignService.UserFeignCreditCardService;
import com.service.creditcard.service.CreditRecordService;
import com.service.creditcard.mapper.CreditRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuwenju
 * @description 针对表【credit_record】的数据库操作Service实现
 * @createDate 2024-04-01 20:57:00
 */
@Slf4j
@Service
public class CreditRecordServiceImpl extends ServiceImpl<CreditRecordMapper, CreditRecord>
        implements CreditRecordService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private UserFeignCreditCardService userFeignCreditCardService;
    @Autowired
    private CreditRecordMapper creditRecordMapper;

    @Override
    public Result verify(ArUserVo arUserVo, String token) {
        log.info("用户信息校验中...{}",arUserVo);
        log.info("我们的token是什么呢------->{}",token);
        arUserVo.setToken(token);
        //验证用户信息
        if (userFeignCreditCardService.theUserIsExist(arUserVo).getCode() == 200) {
            //验证通过将申请信息加入到中间表中(credit_record)
            CreditRecord creditRecord = new CreditRecord();
            creditRecord.setUname(arUserVo.getRealName());
            creditRecord.setIden(arUserVo.getIden());
            creditRecord.setMobile(arUserVo.getMobile());
            this.creditRecordMapper.insert(creditRecord);
        } else {
            return Result.build(null, 201, "用户信息校验未通过！");
        }
        return Result.build(null, 200, "申请成功！工作人员正在快马加鞭的审核~");
    }

    @Override
    public Result myExamine(String token) {
        log.info("token--------->{}",token);
        String loginStr = redisTemplate.opsForValue().get("user:login" + token);
        ArUser arUser = JSON.parseObject(loginStr, ArUser.class);
        QueryWrapper<CreditRecord> creditRecordQueryWrapper = new QueryWrapper<>();
        creditRecordQueryWrapper.eq("uname", arUser.getRealName());
        creditRecordQueryWrapper.eq("iden", arUser.getIden());
        creditRecordQueryWrapper.eq("mobile", arUser.getMobile());
        List<CreditRecord> creditRecords = this.creditRecordMapper.selectList(creditRecordQueryWrapper);
        return Result.build(creditRecords, 200, "查询成功！");
    }


}




